package main

import (
	"fmt"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

var DB *sql.DB

func init() {
	database, err := sql.Open("mysql", "admin:admin123@tcp(192.168.3.103:3306)/Golang_week10")
	if err != nil {
		fmt.Println("连接数据库失败:", err)
		return
	}
	DB = database
}

func insert() {
	r, err := DB.Exec("insert into person(username,gender,email) values(?,?,?)", "Barry", "Male", "Barry@go.net")
	if err != nil {
		fmt.Println("插入数据ERROR:", err)
		return
	}
	fmt.Println(r)
	id, err := r.LastInsertId()
	if err != nil {
		fmt.Println("获取id ERROR:", err)
		return
	}
	fmt.Println(id)
}

func main() {
	insert()
}